package com.hliushi.datasource.generator;

import com.alibaba.fastjson.JSON;
import com.hliushi.datasource.bean.MateriaBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @ClassName Covid19DataGenerator
 * @Description 使用程序模拟生成疫情数据
 * @Author hliushi
 * @Date 2021/7/1 12:14
 * @Version V1.0
 **/
@Component
public class Covid19DataGenerator {


    @Autowired
    private KafkaTemplate kafkaTemplate;
    // 物资名称
    private String[] wzmc = new String[]{"N95口罩/个", "医用外科口罩/个", "84消毒液/瓶", "电子体温计/个", "一次性手套/副", "护目镜/副", "医用防护服/套"};


    // 物资来源
    private String[] wzly = new String[]{"采购", "下拨", "捐赠", "消耗", "需求"};

    /**
     * initialDelay 一开始延迟时间,  fixedDelay 周期时间
     *
     * @param
     * @return
     */
    // @Scheduled(initialDelay = 1000, fixedDelay = 1000)
    public void generator() {

        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            MateriaBean materiaBean = new MateriaBean(wzmc[random.nextInt(wzmc.length)], wzly[random.nextInt(wzly.length)], random.nextInt(1000));
            System.out.println("materiaBean = " + materiaBean);

            // 将生成的疫情物资数据转换为jsonStr, 再发送到Kafka集群
            String jsonStr = JSON.toJSONString(materiaBean);

            kafkaTemplate.send("covid19_wz", random.nextInt(3), jsonStr);
        }
    }

}
